var _NewHost = "http://116.148.228.110:8098"
// var _NewHost = "http://192.168.1.252:7096"
var _Head = {};
var _User;
var _SceneId = 0;

function HttpAjax(url, param, method, dataType, func) {
	$.ajax({
		type: method,
		url: url,
		dataType: dataType,
		data: param,
		contentType: "application/json; charset=utf-8",
		..._Head,
		success: function (data) {
			func(data);
		},
		error: function (jqXHR) {
			func(jqXHR);
		}
	});
}
var token;
// 登录
function _NewLogin(back) {
	HttpAjax(_NewHost + "/api/Login/Login", JSON.stringify({
		"userName": "nbcgda",
		"password": "jdmap@2012"
	}), "POST", "JSON", (dat) => {
		if (dat.code == 200) {
			var data = JSON.parse(dat.data);
			token = data.token;
			_Head = {
				headers: {
					"Authorization": token
				}
			};
			_User = data.user;
			back(data);
		}
	});
}

// 查询主次类型"t_modelMaintype"
function _NewGetMainType(back, name) {
	HttpAjax(_NewHost + "/api/Read/QueryDat", JSON.stringify({
		"tableName": name,
		"field": "*",
		"where": "",
		"page": 1,
		"size": 1000
	}), "POST", "JSON", (dat) => {
		if (dat.code == 200) {
			var data = dat.data.data;
			back(data);
		}
	});

}

// 查询主次类型"t_modelMaintype"
function _NewQueryDat(back, name, field, where, page, size) {
	HttpAjax(_NewHost + "/api/Read/QueryDat", JSON.stringify({
		"tableName": name,
		"field": field,
		"where": where,
		"page": page,
		"size": size
	}), "POST", "JSON", (dat) => {
		if (dat.code == 200) {
			var data = dat.data.data;
			back(data);
		}
	});

}
//所有设备
function _NewGetModellist(back, page, size) {
	HttpAjax(_NewHost + "/api/Read/QueryDatNBDS", JSON.stringify({
		"tableName": "t_devicemodel",
		"field": "*",
		"where": JSON.stringify({
			"senceId": _SceneId * 1,
			"SXTJSON": ""
		}),
		"page": page,
		"size": size
	}), "POST", "JSON", (dat) => {
		if (dat.code == 200) {
			var data = dat.data;
			back(data);
		}
	});

}

//所有设备
function _NewGetModellistEd(back, page, size, typeId) {
	HttpAjax(_NewHost + "/api/Read/QueryDatEd", JSON.stringify({
		"tableName": "t_devicemodel",
		"field": "*",
		"where": JSON.stringify({
			"senceId": _SceneId * 1
		}),
		"typeId": typeId,
		"page": page,
		"size": size
	}), "POST", "JSON", (dat) => {
		if (dat.code == 200) {
			var data = dat.data;
			back(data);
		}
	});

}

// 当前用户场景关联
function _NewGetUserScene(back) {
	HttpAjax(_NewHost + "/api/Read/QueryDat", JSON.stringify({
		"tableName": "t_user_sence",
		"field": "*",

		"where": JSON.stringify({
			"userId": _User.id
		}),
		"page": 1,
		"size": 1
	}), "POST", "JSON", (dat) => {
		if (dat.code == 200) {
			var data = dat.data.data;
			_SceneId = data[0]?.senceId;
			back(data);
		}
	});
}
// 当前用户场景关联
function _NewGetScene(back) {
	HttpAjax(_NewHost + "/api/Read/QueryDat", JSON.stringify({
		"tableName": "t_sences",
		"field": "*",
		"where": JSON.stringify({
			"id": _SceneId
		}),
		"page": 1,
		"size": 1
	}), "POST", "JSON", (dat) => {
		if (dat.code == 200) {
			var data = dat.data.data;
			back(data);
		}	
	});
}

function _NewGetModelTemp(typeId, back) {
	HttpAjax(_NewHost + "/api/Read/GetModelTemplate/" + _SceneId * 1 + "/" + typeId, {}, "GET", "JSON", (dat) => {
		if (dat.code == 200) {
			var data = dat.data.data;
			back(data);
		}
	});

}	